<?php

namespace App\Model;

use App\Model\BaseModel;
use http\QueryString;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Boolean;


class CardModel extends BaseModel
{
    //
    protected $table='card';
    protected $guarded =[];
    public $timestamps = false;

//    关联评论表
    public function comment(){
        return $this ->hasMany(CommentModel::class,'cid','id');
    }

    //    关联图片表
    public function image(){
        return $this -> hasMany(ImageModel::class,'card_id','id');
    }

    //    帖子点赞表
    public function cardLike(){
        return $this -> hasMany(UserLikeModel::class,'cid','id');
    }

//    关联分类表
    public function category(){
        return $this ->hasMany(CategoryModel::class,'id','category_id');
    }


    public function getSendPasteDate($cardId)
    {
     $res =   DB::table('card') ->where(['category_id'=>$cardId]) ->count();

     if($res){
         return ['code' => true,'data' => $res];
     }else{
         return ['code' => false,'data' => '数据为空'];
     }

    }

    public function getTopicContentDate($topic_id,$uuid)
    {

        $res = CardModel::
        withCount(['comment']) ->
        withCount(['cardLike']) ->
        with([
            'comment' => function($query){
                $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
            },
            'image' =>function($query) use($topic_id) {
                $query ->select(['id','card_id','img'])  ->get();
            },
            'cardLike' => function($query) use($uuid) {
                $query -> select(['id','cid'])-> where(['uid' => $uuid])  -> get() ;
            }
        ]) -> where(['is_delete' => 0,'category_id' => $topic_id]) -> paginate(10);

        if($res){
            return ['code' => true,'data' => $res ->makeHidden(['updated_at','sid','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }

    }


    public function getTopicContentHotDate($topic_id,$uuid)
    {
        $res = CardModel::
        withCount(['cardLike']) ->
        withCount(['comment'])->orderBy('comment_count','desc') ->
        with([
            'comment' => function($query){
                $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
            },
            'image' =>function($query) use($topic_id) {
                $query ->select(['id','card_id','img'])  ->get();
            },
            'cardLike' => function($query) use($uuid) {
                $query -> select(['id','cid'])-> where(['uid' => $uuid])  -> get() ;
            }
        ]) -> where(['is_delete' => 0,'category_id' => $topic_id])   -> paginate(10);


        if($res){
            return ['code' => true,'data' => $res ->makeHidden(['updated_at','sid','fav_num','status','is_delete','delete_at','fid',]) ];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }
    }

    public function getTopicContentNewDate( $topic_id,$uuid)
    {

        $res = CardModel::
        withCount(['comment']) ->
        withCount(['cardLike']) ->
        with([
            'comment' => function($query){
                $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
            },
            'image' =>function($query) use($topic_id) {
                $query ->select(['id','card_id','img'])  ->get();
            },
            'cardLike' => function($query) use($uuid) {
                $query -> select(['id','cid'])-> where(['uid' => $uuid])  -> get() ;
            }
        ]) -> where(['is_delete' => 0,'category_id' => $topic_id]) ->orderBy('created_at','desc') -> paginate(10);

        if($res){
            return ['code' => true,'data' => $res ->makeHidden(['updated_at','sid','fav_num','status','is_delete','delete_at','fid',])];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }

    }

    public function getTopicContentDesc( $cid)
    {

        $res = CardModel::where(['id' => $cid]) -> get();

        if($res){
            return ['code' => true,'data' => $res ->makeHidden(['updated_at','sid','fav_num','status','is_delete','delete_at','fid',])];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }
    }

    public function CommentAllNum($cid)
    {
        $res = CardModel::where(['id' => $cid]) -> first();

        if($res){
            return ['code' => true,'data' => $res['total'] ];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }
    }


    public function schoolDescALL($sid,$uuid)
    {
        if(!empty($sid)){
            $res = CardModel::
            withCount(['comment']) ->
            withCount(['cardLike']) ->
            with([
                'comment' => function($query){
                    $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                },
                'image' =>function($query)  {
                    $query ->select(['id','card_id','img'])  ->get();
                },
                'cardLike' => function($query) use($uuid) {
                    $query -> select(['id','cid']) -> where(['uid' =>$uuid] )  ->get();
                },
                'category' => function($query) {

                }

            ]) -> where(['is_delete' => 0,'sid' =>$sid])   -> paginate(10);
            if($res){
                return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }
        }else{
            $res = CardModel::
            withCount(['comment'])->orderBy('comment_count','desc') ->
            withCount(['cardLike']) ->
            with([
                'comment' => function($query){
                    $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                },
                'image' =>function($query)  {
                    $query ->select(['id','card_id','img'])  ->get();
                },
                'cardLike' => function($query) use($uuid) {
                    $query -> select(['id','cid']) -> where(['uid' =>$uuid] )  ->get();
                },
                'category' => function($query) {

                }

            ]) -> where(['is_delete' => 0])   -> paginate(10);
            if($res){
                return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }
        }



    }

    public function schoolDescNew($sid,$uuid)
    {

        if(!empty($sid)){
            $res = CardModel::
            withCount(['comment']) ->
            withCount(['cardLike']) ->
            with([
                'comment' => function($query){
                    $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                },
                'image' =>function($query)  {
                    $query ->select(['id','card_id','img'])  ->get();
                },
                'cardLike' => function($query) use($uuid) {
                    $query -> select(['id','cid']) -> where(['uid' =>$uuid] ) ->get();
                },
                'category' => function($query) {

                }

            ]) -> where(['is_delete' => 0,'sid' =>$sid]) ->orderBy('created_at','desc') -> paginate(10);
            if($res){
                return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',])];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }
        }else{
            $res = CardModel::
            withCount(['comment'])->orderBy('comment_count','desc') ->
            withCount(['cardLike']) ->
            with([
                'comment' => function($query){
                    $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                },
                'image' =>function($query)  {
                    $query ->select(['id','card_id','img'])  ->get();
                },
                'cardLike' => function($query) use($uuid) {
                    $query -> select(['id','cid']) -> where(['uid' =>$uuid] )  ->get();
                },
                'category' => function($query) {

                }

            ]) -> where(['is_delete' => 0]) ->orderBy('created_at','desc') -> paginate(10);
            if($res){
                return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',])];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }
        }



    }

    public function schoolDescHot($sid,$uuid)
    {

        if(!empty($sid)){
            $res = CardModel::
            withCount(['comment'])->orderBy('comment_count','desc') ->
            withCount(['cardLike']) ->
            with([
                'comment' => function($query){
                    $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                },
                'image' =>function($query)  {
                    $query ->select(['id','card_id','img'])  ->get();
                },
                'cardLike' => function($query) use($uuid) {
                    $query -> select(['id','cid']) -> where(['uid' =>$uuid] ) ->get();
                },
                'category' => function($query) {

                }
            ]) -> where(['is_delete' => 0,'sid' =>$sid])   -> paginate(10);
            if($res){
                return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',])];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }
        }else{
            $res = CardModel::
            withCount(['comment'])->orderBy('comment_count','desc') ->
            withCount(['cardLike']) ->
            with([
                'comment' => function($query){
                    $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                },
                'image' =>function($query)  {
                    $query ->select(['id','card_id','img'])  ->get();
                },
                'cardLike' => function($query) use($uuid) {
                    $query -> select(['id','cid']) -> where(['uid' =>$uuid] )->get();
                },
                'category' => function($query) {

                }
            ]) -> where(['is_delete' => 0])   -> paginate(10);
            if($res){
                return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',])];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }
        }

    }

    public function getALLHomeDate()
    {

        $res = CardModel::
        withCount(['comment'])->orderBy('comment_count','desc') ->
        withCount(['cardLike']) ->
        with([
            'comment' => function($query){
                $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
            },
            'image' =>function($query)  {
                $query ->select(['id','card_id','img'])  ->get();
            },
//            'cardLike' => function($query) {
//                $query -> select(['id','cid']) ->get() ;
//            }
            'category' => function($query) {

            }
        ]) -> where(['is_delete' => 0])   -> paginate(10);
        if($res){
            return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }

    }

    public function getHomeMySchoolDate($uuid)
    {
        //查询我的大学
        $school = UserModel::find($uuid);

        $res = CardModel::
        withCount(['comment'])->orderBy('comment_count','desc') ->
        withCount(['cardLike']) ->
        with([
            'comment' => function($query){
                $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
            },
            'image' =>function($query)  {
                $query ->select(['id','card_id','img'])  ->get();
            },
//            'cardLike' => function($query) {
//                $query -> select(['id','cid']) ->get() ;
//            }
            'category' => function($query) {

            }
        ]) -> where(['is_delete' => 0,'sid' => $school['sid']])   -> paginate(10);
        if($res){
            return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }

    }

    public function getHomeMyFocusSchoolDate($uuid)
    {

        $like = SchoolKeepModel::where('uid',$uuid) ->get(['sid']);

        $res = CardModel::whereIn('sid',$like) ->
        withCount(['comment']) ->
        withCount(['cardLike']) ->
        with([
            'comment' => function($query){
                $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
            },
            'image' =>function($query)  {
                $query ->select(['id','card_id','img'])  ->get();
            },
//            'cardLike' => function($query) {
//                $query -> select(['id','cid']) ->get() ;
//            }
            'category' => function($query) {

            }
        ]) -> where(['is_delete' => 0])   -> paginate(10);


        if($res){
            return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }

    }

    public function getHomeNewDate()
    {
        $res = CardModel::
        withCount(['comment'])->orderBy('comment_count','desc') ->
        withCount(['cardLike']) ->
        with([
            'comment' => function($query){
                $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
            },
            'image' =>function($query)  {
                $query ->select(['id','card_id','img'])  ->get();
            },
//            'cardLike' => function($query) {
//                $query -> select(['id','cid']) ->get() ;
//            }
            'category' => function($query) {

            }
        ]) -> where(['is_delete' => 0])  -> paginate(10);


        if($res){
            return ['code' => true,'data' => $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) ];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }
    }

    public function getHomeHotDate()
    {

        $res = CardModel::
        withCount(['comment'])->orderBy('comment_count','desc') ->
        withCount(['cardLike']) ->
        with([
            'comment' => function($query){
                $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
            },
            'image' =>function($query) {
                $query ->select(['id','card_id','img'])  ->get();
            },
//            'cardLike' => function($query) {
//                $query -> select(['id','cid']) ->get() ;
//            }
            'category' => function($query) {

            }
        ]) -> where(['is_delete' => 0]) ->orderBy('created_at','desc') -> paginate(10);

        if($res){
            return ['code' => true,'data' => $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',])];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }

    }

    public function AddSendPasteContent($uuid, $uNmae, $uhead_img, $sid, $school_name, $content, $is_anonymity, $cid, $sex)
    {
        $res = DB::table('card') ->insertGetId([
            'uid' => $uuid,
            'u_nickname' => $uNmae,
            'head_img' => $uhead_img,
            'school_name' => $school_name,
            'sid' => $sid,
            'content' => $content,
            'category_id' => $cid,
            'sex' => $sex,
            'is_niming' => $is_anonymity,
            'created_at' => date('Y-m-d H:i:s')
        ]);
        if($res > 0){

//            对应学校发帖数量加1
            $school = SchoolModel::find($sid);
            $school -> total_card = $school -> total_card + 1;
            $school -> save();

            return ['code' => true,'data' => $res];
        }else{
            return ['code' => false,'data' => '添加失败！'];
        }
    }

    public function AddSendPasteImage($uuid,$cid,$card_id,$image_adar)
    {
        $res = DB::table('image') ->insertGetId([
            'uid' => $uuid,
            'cid' => $cid,
            'img' => $image_adar,
            'card_id' => $card_id,
            'created_at' =>  date('Y-m-d H:i:s')
        ]);
        if($res > 0){
            return ['code' => true,'data' => 1];
        }else{
            return ['code' => false,'data' => 0];
        }
    }

    public function getMyPost($uuid, $toid)
    {
        if(is_null($toid) || empty($toid) || $toid == 'undefined'){//null（查询自己的）
            $res = CardModel::
            withCount(['comment']) ->
            withCount(['cardLike']) ->
            with([
                'comment' => function($query){
                    $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                },
                'image' =>function($query)  {
                    $query ->select(['id','card_id','img'])  ->get();
                },
//                'cardLike' => function($query) {
//                    $query -> select(['id','cid']) ->get() ;
//                },
                'category' =>function($query) {

                },

            ]) -> where(['is_delete' => 0,'uid' =>$uuid])   -> paginate(10);
            if($res){
                return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }
        }else{
            $res = CardModel::
            withCount(['comment']) ->
            withCount(['cardLike']) ->
            with([
                'comment' => function($query){
                    $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                },
                'image' =>function($query)  {
                    $query ->select(['id','card_id','img'])  ->get();
                },
//                'cardLike' => function($query) {
//                    $query -> select(['id','cid']) ->get() ;
//                },
                'category' =>function($query) {

                },

            ]) -> where(['is_delete' => 0,'uid' =>$toid])   -> paginate(10);
            if($res){
                return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }
        }


    }


}
